﻿Option Strict Off

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.ComponentModel.DataAnnotations
Imports System.Linq
Imports System.ServiceModel.DomainServices.Hosting
Imports System.ServiceModel.DomainServices.Server
Imports WCF_RIA_Project.LightSwitchApplication.Implementation
Imports System.Data.EntityClient
Imports System.Web.Configuration
Namespace WCF_RIA_Project
    ' This class is used as the class that is returned
    ' This can have any 'shape' you desire
    ' Make sure this is outside of the WCF_RIA_Service class
    ' but inside the WCF_RIA_Project namespace

    Public Class WCF_RIA_Service
        Inherits DomainService
        ' This Context property is code that connects to the LightSwitch database
        ' The code in the Database connection region can be reused as it is 
#Region "Database connection"
        Private m_context As ApplicationData
        Public ReadOnly Property Context() As ApplicationData 'ApplicationDataObjectContext
            Get
                If Me.m_context Is Nothing Then
                    Dim connString As String = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("_IntrinsicData").ConnectionString
                    Dim builder As New EntityConnectionStringBuilder()
                    builder.Metadata = "res://*/ApplicationData.csdl|res://*/ApplicationData.ssdl|res://*/ApplicationData.msl"
                    builder.Provider = "System.Data.SqlClient"
                    builder.ProviderConnectionString = connString
                    Me.m_context = New ApplicationData(builder.ConnectionString) 'ApplicationDataObjectContext(builder.ConnectionString)
                End If
                Return Me.m_context
            End Get
        End Property
#End Region

        ' Override the Count method in order for paging to work correctly
        Protected Overrides Function Count(Of T)(query As IQueryable(Of T)) As Integer
            Return query.Count()
        End Function

        <Query(IsDefault:=True)> _
        Public Function GetAnimals() As IQueryable(Of Dashboard)
            ' Get all the Orders
            ' Shape the results into the 
            ' EnhancedFlowerShopOrder class
            ' The Order ID
            ' The Order Date
            ' The first name of the Customer
            ' The last name of the Customer
            ' The order Total
            ' Get all order details lines of the Order
            ' Group the products in the Order Details
            ' Shape a new entity
            ' Create a total property that is the Quantity times the
            ' Product price
            ' Add the sum of all the TotalOrders
            Dim timenow As DateTime = Now
            'Dim timedays As TimeSpan
            Dim colAnimals = From A In Me.Context.Animals
                Select New Dashboard With {
                .ID = A.Id,
                .ArrivalDate = If(A.DateBroughtIn IsNot Nothing, A.DateBroughtIn, Now),
                .AnimalName = A.Name,
                .ArrivalDays = System.Data.Objects.EntityFunctions.DiffDays(If(A.DateBroughtIn IsNot Nothing, A.DateBroughtIn, Now), Now),
                .Archived = If(A.Archived IsNot Nothing, A.Archived, 0),
                .Sex = A.Sex,
                .RemoveFromMissingMicrochip = A.RemoveFromMissingMicrochip,
                .RemoveFromMissingNeuter = A.RemoveFromMissingNeuter,
                .LastActiveStatus = A.ActiveMovementType.Title,
                .MicroChipID = A.MicrochipID,
                .MissingNeuter = If(A.RemoveFromMissingNeuter = True, False, If(A.SpayNeuterDate IsNot Nothing, False, If(A.UnknownSpayNeuterDate = True, False, True)))
            }

            'Key .LastName = Order.FlowerShopCustomer.LastName, _
            'Key .OrderTotal = (From g In From FlowerShopOrderDetail In Order.FlowerShopOrderDetailGroup FlowerShopOrderDetail By FlowerShopOrderDetail.FlowerShopProductNew With { _
            '		Key .TotalOrder = g.Sum(Function(x) x.Quantity) * g.Sum(Function(x) x.FlowerShopProduct.Price) _
            '	}).Sum(Function(x) x.TotalOrder) _
            Return colAnimals
        End Function

        Public Sub UpdateCombinedProducts(dash As Dashboard)
            ' Get the External Product
            Dim colExternalProducts = (From ExternalProducts In Me.Context.Animals Where ExternalProducts.Id = dash.ID).FirstOrDefault()
            If colExternalProducts IsNot Nothing Then
                ' Update the Product
                colExternalProducts.Name = dash.AnimalName
                colExternalProducts.DateBroughtIn = dash.ArrivalDate
                colExternalProducts.RemoveFromMissingNeuter = dash.RemoveFromMissingNeuter
                colExternalProducts.RemoveFromMissingMicrochip = dash.RemoveFromMissingMicrochip
                Me.Context.SaveChanges()
            End If
        End Sub

    End Class

    Public Class Dashboard
        <Key()> _
        Public Property ID() As Integer
            Get
                Return m_ID
            End Get
            Set(value As Integer)
                m_ID = value
            End Set
        End Property
        Private m_ID As Integer
        Public Property ArrivalDate() As DateTime
            Get
                Return m_ArrivalDate
            End Get
            Set(value As DateTime)
                m_ArrivalDate = value
            End Set
        End Property
        Private m_ArrivalDate As DateTime
        Public Property AnimalName() As String
            Get
                Return m_AnimalName
            End Get
            Set(value As String)
                m_AnimalName = value
            End Set
        End Property
        Private m_AnimalName As String
        Public Property ArrivalDays() As Integer
            Get
                Return m_ArrivalDays
            End Get
            Set(value As Integer)
                m_ArrivalDays = value
            End Set
        End Property
        Private m_ArrivalDays As Integer
        Public Property Archived() As Integer
            Get
                Return m_Archived
            End Get
            Set(value As Integer)
                m_Archived = value
            End Set
        End Property
        Private m_Archived As Integer
        Public Property Sex() As String
            Get
                Return m_Sex
            End Get
            Set(value As String)
                m_Sex = value
            End Set
        End Property
        Private m_Sex As String
        Public Property RemoveFromMissingMicrochip() As Boolean
            Get
                Return m_RemoveFromMissingMicrochip
            End Get
            Set(value As Boolean)
                m_RemoveFromMissingMicrochip = value
            End Set
        End Property
        Private m_RemoveFromMissingMicrochip As Boolean
        Public Property RemoveFromMissingNeuter() As Boolean
            Get
                Return m_RemoveFromMissingNeuter
            End Get
            Set(value As Boolean)
                m_RemoveFromMissingNeuter = value
            End Set
        End Property
        Private m_RemoveFromMissingNeuter As Boolean
        Public Property LastActiveStatus() As String
            Get
                Return m_LastActiveStatus
            End Get
            Set(value As String)
                m_LastActiveStatus = value
            End Set
        End Property
        Private m_LastActiveStatus As String
        Public Property MicroChipID() As String
            Get
                Return m_MicroChipID
            End Get
            Set(value As String)
                m_MicroChipID = value
            End Set
        End Property
        Private m_MicroChipID As String
        Public Property MissingNeuter() As Boolean
            Get
                Return m_MissingNeuter
            End Get
            Set(value As Boolean)
                m_MissingNeuter = value
            End Set
        End Property
        Private m_MissingNeuter As Boolean
    End Class
End Namespace